package com.goods.business.service.imp;

import com.goods.business.converter.ProductConverter;
import com.goods.business.mapper.ProductMapper;
import com.goods.business.mapper.ProductStockMapper;
import com.goods.business.service.ProductService;
import com.goods.common.error.BusinessCodeEnum;
import com.goods.common.error.BusinessException;
import com.goods.common.model.business.Product;
import com.goods.common.model.business.ProductCategory;
import com.goods.common.model.business.ProductStock;
import com.goods.common.utils.ListPageUtils;
import com.goods.common.vo.business.ProductCategoryVO;
import com.goods.common.vo.business.ProductStockVO;
import com.goods.common.vo.business.ProductVO;
import com.goods.common.vo.system.PageVO;
import io.swagger.models.auth.In;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.*;

@Service
public class ProductServiceImpl implements ProductService {
    @Resource
    private ProductMapper productMapper;
    @Resource
    private ProductStockMapper productStockMapper;

    @Override
    public PageVO<ProductVO> findProductList(Map<String,Object> queryMap) {
        //是否删除:1物资正常,0:物资回收,2:物资审核中
        Example example = new Example(Product.class);
        if (queryMap.get("categoryId") != null && !"".equals(queryMap.get("categoryId"))){
            example.createCriteria().andEqualTo("categoryId", queryMap.get("categoryId"));
        }

        if (queryMap.containsKey("categorys") && !"".equals("categorys")){
            String categorys = (String)queryMap.get("categorys");
            String[] split = categorys.split(",");

            if (split.length >0){
                switch (split.length){
                    case 1:
                        example.createCriteria().andEqualTo("oneCategoryId",split[0]);
                        break;
                    case 2:
                        example.createCriteria().andEqualTo("twoCategoryId",split[1]);
                        break;
                    case 3:
                        example.createCriteria().andEqualTo("threeCategoryId",split[2]);
                        break;
                }
            }
        }

        if (queryMap.get("status") != null && !"".equals(queryMap.get("status"))) {
            example.createCriteria().andEqualTo("status", queryMap.get("status"));
        }
        if (queryMap.containsKey("name") && !"".equals(queryMap.get("name"))) {
            example.createCriteria().andEqualTo("name", queryMap.get("name"));
        }
        List<Product> products = productMapper.selectByExample(example);
        List<ProductVO> productVOList = ProductConverter.converterToProductVO(products);
        if (!CollectionUtils.isEmpty(productVOList)){
            String pageSize = (String)queryMap.get("pageSize");
            String pageNum = (String)queryMap.get("pageNum");
            List<Product> page = ListPageUtils.page(products, Integer.parseInt(pageSize), Integer.parseInt(pageNum));
            return new PageVO<ProductVO>(page.size(), productVOList);
        }
        return new PageVO<ProductVO>(0L, null);
    }
        //添加物资资料
        @Override
        public void add (ProductVO productVO){
            Product product = new Product();
            BeanUtils.copyProperties(productVO, product);
            product.setCreateTime(new Date());
            product.setModifiedTime(new Date());
            product.setStatus(2);
            product.setOneCategoryId(productVO.getOneCategoryId());
            product.setTwoCategoryId(productVO.getTwoCategoryId());
            product.setThreeCategoryId(productVO.getThreeCategoryId());
            product.setSort(productVO.getSort());
            String string = UUID.randomUUID().toString();
            product.setPNum(string);
            productMapper.insert(product);
        }
    //编辑物资资料
    @Override
    public ProductVO edit(Long id) {
        Product product = productMapper.selectByPrimaryKey(id);
        ProductVO productVO = ProductConverter.converterToProductVOSimple(product);
        return productVO;
    }
    //编辑物资资料
    @Override
    public void update(Long id, ProductVO productVO) throws BusinessException {
        //根据id查询 看是否存在分类的信息
        Product product = productMapper.selectByPrimaryKey(id);
        if (product == null){
            throw new BusinessException(BusinessCodeEnum.PARAMETER_ERROR,"数据不存在!");
        }else {
            Product product1 = new Product();
            BeanUtils.copyProperties(productVO,product1);
            product1.setId(id);
            product1.setModifiedTime(new Date());
            productMapper.updateByPrimaryKey(product1);
        }
    }
    //移入回收站  是否删除:0物资正常,1:物资回收,2:物资审核中
    @Override
    public void remove(Long id) throws BusinessException {
        Product product = productMapper.selectByPrimaryKey(id);
        if (product == null){
            throw new BusinessException(BusinessCodeEnum.PARAMETER_ERROR,"该参数不存在!");
        }
        product.setStatus(1);
        product.setId(id);
        productMapper.updateByPrimaryKey(product);
    }
    //从回收站中恢复
    @Override
    public void back(Long id) throws BusinessException {
        Product product = productMapper.selectByPrimaryKey(id);
        if (product == null){
            throw new BusinessException(BusinessCodeEnum.PARAMETER_ERROR,"该参数不存在!");
        }
        product.setStatus(0);
        product.setId(id);
        productMapper.updateByPrimaryKey(product);
    }
    //删除该物资资料
    @Override
    public void delete(Long id) throws BusinessException {
        Product product = productMapper.selectByPrimaryKey(id);
        if (product == null){
            throw new BusinessException(BusinessCodeEnum.PARAMETER_ERROR,"该参数不存在!");
        }
        productMapper.delete(product);
    }
    //物资资料的审核
    @Override
    public void publish(Long id) throws BusinessException {
        Product product = productMapper.selectByPrimaryKey(id);
        if (product == null){
            throw new BusinessException(BusinessCodeEnum.PARAMETER_ERROR,"该参数不存在!");
        }
        product.setStatus(0);
        product.setId(id);
        productMapper.updateByPrimaryKey(product);
    }
    //获取所有的物资列表
    @Override
    public PageVO<ProductStockVO> findProducts(Map<String, Object> queryMap) {
        //是否删除:1物资正常,0:物资回收,2:物资审核中
        Example example = new Example(Product.class);
        if (queryMap.get("categoryId") != null && !"".equals(queryMap.get("categoryId"))) {
            example.createCriteria().andEqualTo("categoryId", queryMap.get("categoryId"));
        }
        if (queryMap.get("name") != null && !"".equals(queryMap.get("name"))) {
            example.createCriteria().andLike("name", "%" + queryMap.get("name") + "%");
        }
        List<Product> productList = productMapper.selectByExample(example);
        List<ProductStockVO> productStockVOS = ProductConverter.converterToProductStockVOList(productList);
        String pageSize = (String)queryMap.get("pageSize");
        String pageNum = (String)queryMap.get("pageNum");
        List<Product> page = ListPageUtils.page(productList, Integer.parseInt(pageSize), Integer.parseInt(pageNum));
        return new PageVO<ProductStockVO>(page.size(), productStockVOS);
    }
    //获取所有物资的库存信息
    @Override
    public List<ProductStockVO> findAllStocks() {
        List<Product> productList = productMapper.selectAll();
        List<ProductStockVO> productStockVOList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(productList)){
            for (Product product : productList) {
                String pNum = product.getPNum();
                Example example1 = new Example(ProductStock.class);
                example1.createCriteria().andEqualTo("pNum",pNum);
                ProductStockVO productStockVO = new ProductStockVO();
                productStockVO.setImageUrl(product.getImageUrl());
                productStockVO.setName(product.getName());
                productStockVO.setModel(product.getModel());
                productStockVO.setUnit(product.getUnit());
                productStockVO.setId(product.getId());
                List<ProductStock> productStocks = productStockMapper.selectByExample(example1);
                for (ProductStock productStock : productStocks) {
                    productStockVO.setStock(productStock.getStock());
                }
                productStockVOList.add(productStockVO);
            }
        }
        return productStockVOList;
    }

    @Override
    public PageVO<ProductStockVO> findProductStocks(Map<String, Object> queryMap) {
        Example example = new Example(Product.class);
        //通过物资名称（模糊）进行查询
        if (queryMap.get("name") != null && !"".equals(queryMap.get("name"))) {
            example.createCriteria().andLike("name","%" + queryMap.get("name") + "%");
        }
        //通过物资分类id查询
        if (queryMap.containsKey("categorys") && !"".equals("categorys")){
            String categorys = (String)queryMap.get("categorys");
            String[] split = categorys.split(",");

            if (split.length >0){
                switch (split.length){
                    case 1:
                        example.createCriteria().andEqualTo("oneCategoryId",split[0]);
                        break;
                    case 2:
                        example.createCriteria().andEqualTo("twoCategoryId",split[1]);
                        break;
                    case 3:
                        example.createCriteria().andEqualTo("threeCategoryId",split[2]);
                        break;
                }
            }
        }
        List<Product> productList = productMapper.selectByExample(example);
        List<ProductStockVO> productStockVOList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(productList)){
            for (Product product : productList) {
                String pNum = product.getPNum();
                Example example1 = new Example(ProductStock.class);
                example1.createCriteria().andEqualTo("pNum",pNum);
                ProductStockVO productStockVO = new ProductStockVO();
                productStockVO.setImageUrl(product.getImageUrl());
                productStockVO.setName(product.getName());
                productStockVO.setModel(product.getModel());
                productStockVO.setUnit(product.getUnit());
                productStockVO.setId(product.getId());
                List<ProductStock> productStocks = productStockMapper.selectByExample(example1);
                for (ProductStock productStock : productStocks) {
                    productStockVO.setStock(productStock.getStock());
                }
                productStockVOList.add(productStockVO);
            }
            if (!CollectionUtils.isEmpty(productStockVOList)){
                String pageSize = (String)queryMap.get("pageSize");
                String pageNum = (String)queryMap.get("pageNum");
                List<ProductStockVO> page = ListPageUtils.page(productStockVOList, Integer.parseInt(pageSize), Integer.parseInt(pageNum));
                return new PageVO<>(page.size(),productStockVOList);
            }
        }
        return new PageVO<>(0L,null);
    }
}
